<?php
namespace Admin\Controller;

use Util\AccessController;

class UserController extends AccessController {

    public function Index() {

//        dump(passport_encrypt('89.68',C('JM_KEY')));

        $LS = Service('User', 'user_wx_lable_view');
        $WEHE = LIKEGET_ADMIN($_GET);
        unset($WEHE['lable_id']);
        $_GET['time1'] ? 1 : $_GET['time1'] = '1993-1-1 00:0:0';
        $_GET['time2'] ? 1 : $_GET['time2'] = '9999-1-1 00:0:0';
        if($_GET['lable_id']){
            $WEHE['lable_id'] =array('in',$_GET['lable_id']);
        }
        $WEHE['user_time'] = array('between', array($_GET['time1'], $_GET['time2']));
        $order = I('get.order') != '' && I('get.sort') != '' ? PageSort(I('get.order'), I('get.sort')) : 'login_time desc';
        $files = '*';
        $count = $LS->count($WEHE);
        $OnePageShow = 10;
        $Page = new \Think\NewPage($count, $OnePageShow);
        $show = $Page->show(); // 分页显示输出
        $limit = $Page->limit();
        $DATA = $LS->query($WEHE, $limit, $files, $order);
        $this->CSDATA_JOIN = json_encode($_GET);
        $this->CSDATA = $_GET;
        $this->show = $show;
        $this->data = $DATA;
        $this->count = $count;

        //信息数据
        $SendData = $LS->query($WEHE,null,'user_wx');
        $this->SendData =json_encode($SendData);

        //微信标签
        $LS1 = Service('Book', 'region');
        $CSpdata = $LS1->find(array('region_name' => '微信公共号标签'));
        $CSsondata = $LS1->query(array('region_fid' => $CSpdata['region_id']));
        $this->WX_LABLE = $CSsondata;

        //所有有效优惠劵
        $where['STATE']= 2;
        $where['ETIME'] =  array('GT',date('Y-m-d'));
        $LS2 = Service('Card','card');
        $coupon_data = $LS2->query($where);
        $this->COUPON_DATA =$coupon_data;


        //城市
        $LS1 = Service('M', 'u_user');
        $group = 'user_area_shi';
        $CSsondata = $LS1->query(null,null,'user_area_shi','user_area_shi', $group);
        $this->CITY = $CSsondata;
        $this->display();
    }

    public function update() {
        if (IS_POST) {
            $us = Service('User', 'user');
            $data = I("post.");
            if ($us->update($data)) {
                $vo['info'] = "修改成功！";
                $vo['state'] = 1;
            } else {
                $vo['info'] = "修改失败！";
                $vo['state'] = 0;
            }
            $this->ajaxReturn($vo);
        }
    }

    public function WxAllUser() {
        $LS = Service('User', 'u_user');
        $where['user_wx_name'] = array('exp', 'is null');
//        $data = $LS->query($where, null, 'user_wx');
        $data = $LS->query(null, null, 'user_wx');
        $this->count = count($data);
        $this->data = json_encode($data);
        $this->display();

    }

    /*
     * 获取所有微信用户openid
     */
    public function GetAllWxUser() {
        $wechat = new \Util\WechatTicket();
        $wechat->set(C('WX_APPID'), C('WX_SECRET'));
        $data = $wechat->GetAllOpenid();
        $LS = Service('User', 'u_user');
        $userData = $LS->query(null, null, 'user_wx');
        $save = array();
        $OK = 0;
        foreach ($data['data']['openid'] as $k => $v) {
            $tmp['user_wx'] = $v;
            $tmp['user_id'] = 'WX' . uniqid() . '_' . rand(0, 100) . $k;
            if ($this->inUser($v, $userData) == 0) {
                $save[] = $tmp;
            }
        }
        $save ? $OK = $LS->AddAll($save) : 1;
        $OK ? $this->success('共拉取openid数:' . $OK) : $this->error('拉取失败');
    }

    /*
     * 是否存在openid
     * return 1 存在
     * return 0 不存在
     */
    public function inUser($openid, $data) {
        foreach ($data as $k => $v) {
            if ($v['user_wx'] == $openid) {
                return 1;
            }
        }
        return 0;
    }

    /*
     * 完善用户信息
     */
    public function WsUserInfo() {
        $wechat = new \Util\WechatTicket();
        $wechat->set(C('WX_APPID'), C('WX_SECRET'));
        $data = $wechat->UserBaseMsg($_POST['OPENID']);
        $LS = Service('M', 'u_user');
        $where['user_wx'] = $_POST['OPENID'];
        $data1['user_time'] = date('Y-m-d H:i:s', $data['subscribe_time']);
        $data1['user_image'] = $data['headimgurl'];
        $data1['user_wx_name'] = $data['nickname'];
        $data1['user_gender'] = $data['sex'];
        $data1['user_area_sheng'] = $data['province'];
        $data1['user_area_shi'] = $data['city'];
        //用户标签
        $data1['user_wx_lable'] = json_encode($data['tagid_list']);
        $OK = $LS->update($where, $data1);
        echo $OK;
    }

    /*
     * 通过openid获取用户信息
     */
    public function GetWxUserInfo() {
        $wechat = new \Util\WechatTicket();
        $wechat->set(C('WX_APPID'), C('WX_SECRET'));
        $data = $wechat->UserBaseMsg('on7J8046NthBty6rQttBD-uQUSwg');
        dump($data);
    }

    public function AddWxUserIndex() {
        $LS = Service('User', 'openid');
        $count = $LS->count();
        $OPENIDALL = $LS->query();
        $this->OPENIDALL = json_encode($OPENIDALL);
        $this->count = $count;
        $this->display();
    }

    public function AddWxUser() {
        $wechat = new \Util\WechatTicket();
        $wechat->set(C('WX_APPID'), C('WX_SECRET'));
        $data = $wechat->UserBaseMsg($_POST['OPENID']);
        $LS = Service('User', 'u_user');
        $OK1 = $LS->find(array('user_wx' => $data['openid']));
        $OK = 0;
        if (!$OK1 && $data['openid']) {
            $save['user_id'] = 'WX_' . uniqid() . rand(0, 9);
            $save['user_image'] = $data['headimgurl'];
            $save['user_gender'] = $data['sex'];
            $save['user_wx_name'] = $data['nickname'];
            $save['user_time'] = date('Y-m-d H:i:s');
            $save['user_area_sheng'] = $data['province'];
            $save['user_area_shi'] = $data['city'];
            $save['user_wx'] = $data['openid'];
            $OK = $LS->insert($save);
        }
        echo $OK;
    }

}